package org.ytor.core.querygen.support;

import org.springframework.stereotype.Component;
import org.ytor.common.util.Strs;
import org.ytor.core.querygen.AbsWhere;
import org.ytor.core.querygen.token.Token;
import org.ytor.core.sqlflow.builder.support.WhereBuilder;

/**
 * created by yangtong on 2025/8/11 14:28:52
 * <br/>
 * 小于等于 age_lt=23 -> age <= 23
 */
@Component
public class LeWhere extends AbsWhere {
    private static final String SUFFIX = "_le";

    @Override
    public Boolean isMatch(Token token) {
        return Strs.isNotEmpty(token.getValue()) && token.getKey().endsWith(SUFFIX);
    }

    @Override
    protected WhereBuilder positive(WhereBuilder where, Token token) {
        String col = Strs.toUnderline(token.getKey().substring(0, token.getKey().length() - SUFFIX.length()));
        return (WhereBuilder) where.le(col, token.getValue());
    }

    @Override
    protected WhereBuilder negate(WhereBuilder where, Token token) {
        String col = Strs.toUnderline(token.getKey().substring(0, token.getKey().length() - SUFFIX.length()));
        return (WhereBuilder) where.gt(col, token.getValue());
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE + 1;
    }

    @Override
    public WhereBuilder apply(WhereBuilder where, Token token) {
        return positiveOrNegate(where, token);
    }
}
